﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BProduct : Common.CDBase
    {
        BCategory_Product _BCategory_Product = new BCategory_Product();
        /// <summary>
        /// Thêm sản phẩm
        /// </summary>
        /// <param name="_OProduct"></param>
        /// <returns></returns>
        public bool Add(OProduct _OProduct, List<int> lstCatID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name",SqlDbType.NVarChar)
                                      , new SqlParameter("@Description",SqlDbType.NVarChar)
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)
                                      , new SqlParameter("@Price",SqlDbType.Float)
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@ModifiedDate",SqlDbType.DateTime)
                                      , new SqlParameter("@SeoUrl",SqlDbType.NVarChar)                                      
                                      , new SqlParameter("@UserCreate",SqlDbType.Int)
                                      , new SqlParameter("@LangID",SqlDbType.VarChar)
                                      , new SqlParameter("@Status",SqlDbType.NVarChar)
                                      , new SqlParameter("@Images",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OProduct.Name;
            sqlParam[1].Value = _OProduct.Description;
            sqlParam[2].Value = _OProduct.FeaturedImage;
            sqlParam[3].Value = _OProduct.Price;
            sqlParam[4].Value = _OProduct.CreateDate;
            sqlParam[5].Value = _OProduct.ModifiedDate;
            sqlParam[6].Value = _OProduct.SeoUrl;
            sqlParam[7].Value = _OProduct.CreateUser;
            sqlParam[8].Value = _OProduct.LangID;
            sqlParam[9].Value = _OProduct.Status;
            sqlParam[10].Value = _OProduct.Images;
            if (RunProcedure("sp_tblProduct_add", sqlParam))
            {
                int IDProduct = this.GetIDLastRecord();
                if (IDProduct > 0)
                {
                    if (lstCatID != null)
                    {
                        foreach (int IDCat in lstCatID)
                        {
                            _BCategory_Product.Add(IDCat, IDProduct);
                        }
                    }
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Lấy về danh sách đối tượng OProduct theo ProductID, nếu ProductID=0 lấy về 50 sản phẩm mới nhất
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public IList<OProduct> Get(int ProductID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@ProductID", SqlDbType.Int) };
            sqlParam[0].Value = ProductID;
            return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
        }
        /// <summary>
        /// Lấy về danh sách sản phẩm theo category
        /// </summary>
        /// <param name="CatID"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OProduct> Get(int CatID, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CatID", SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = CatID;
            sqlParam[1].Value = PageSize;
            return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
        }
        public IList<OProduct> Get(string Name, string Status, string LandID, int CatID, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name", SqlDbType.NVarChar),
                                        new SqlParameter("@Status",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Name;
            sqlParam[1].Value = Status;            
            sqlParam[2].Value = LandID;
            sqlParam[3].Value = CatID;
            sqlParam[4].Value = Order;
            sqlParam[5].Value = OrderBy;
            sqlParam[6].Value = PageIndex;
            sqlParam[7].Value = PageSize;
            return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
        }
        /// <summary>
        /// Lấy về danh sách đối tượng OProduct
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="FromCreateDate"></param>
        /// <param name="ToCreateDate"></param>
        /// <param name="Status"></param>
        /// <param name="LandID"></param>
        /// <param name="CatID"></param>
        /// <param name="Order"></param>
        /// <param name="OrderBy"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public IList<OProduct> Get(string Name, DateTime FromCreateDate, DateTime ToCreateDate, string Status, string LandID, int CatID, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name", SqlDbType.NVarChar),
                                        new SqlParameter("@FromCreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@ToCreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@Status",SqlDbType.NVarChar),
                                        new SqlParameter("@LangID",SqlDbType.VarChar),
                                        new SqlParameter("@CatID",SqlDbType.Int),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Name;
            sqlParam[1].Value = FromCreateDate;
            sqlParam[2].Value = ToCreateDate;
            sqlParam[3].Value = Status;
            sqlParam[4].Value = LandID;
            sqlParam[5].Value = CatID;
            sqlParam[6].Value = Order;
            sqlParam[7].Value = OrderBy;
            sqlParam[8].Value = PageIndex;
            sqlParam[9].Value = PageSize;
            return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
        }
        public IList<OProduct> Search(string Name, string Order, string OrderBy, Nullable<int> PageIndex, Nullable<int> PageSize)
        {
            if (PageIndex != null && PageSize != null)
            {
                SqlParameter[] sqlParam = { new SqlParameter("@Name", SqlDbType.NVarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
                sqlParam[0].Value = Name;
                sqlParam[1].Value = Order;
                sqlParam[2].Value = OrderBy;
                sqlParam[3].Value = PageIndex;
                sqlParam[4].Value = PageSize;
                return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
            }
            else
            {
                SqlParameter[] sqlParam = { new SqlParameter("@Name", SqlDbType.NVarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),};
                sqlParam[0].Value = Name;
                sqlParam[1].Value = Order;
                sqlParam[2].Value = OrderBy;
                return Common.Common.ConvertTo<OProduct>(RunProcedureGet("sp_tblProduct_get", sqlParam));
            }
        }
        /// <summary>
        /// Cập nhật sản phẩm
        /// </summary>
        /// <param name="_OProduct"></param>
        /// <returns></returns>
        public bool Update(OProduct _OProduct, List<int> lstCatID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@ProductID",SqlDbType.Int)
                                      , new SqlParameter("@Name",SqlDbType.NVarChar)
                                      , new SqlParameter("@Description",SqlDbType.NVarChar)
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)
                                      , new SqlParameter("@Price",SqlDbType.Float)
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@ModifiedDate",SqlDbType.DateTime)                                      
                                      , new SqlParameter("@SeoUrl",SqlDbType.NVarChar)
                                      , new SqlParameter("@LangID",SqlDbType.VarChar)
                                      , new SqlParameter("@Status",SqlDbType.NVarChar)
                                      , new SqlParameter("@Images",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OProduct.ProductID;
            sqlParam[1].Value = _OProduct.Name;
            sqlParam[2].Value = _OProduct.Description;
            sqlParam[3].Value = _OProduct.FeaturedImage;
            sqlParam[4].Value = _OProduct.Price;
            sqlParam[5].Value = _OProduct.CreateDate;
            sqlParam[6].Value = _OProduct.ModifiedDate;
            sqlParam[7].Value = _OProduct.SeoUrl;
            sqlParam[8].Value = _OProduct.LangID;
            sqlParam[9].Value = _OProduct.Status;
            sqlParam[10].Value = _OProduct.Images;
            if (RunProcedure("sp_tblProduct_update", sqlParam))
            {
                _BCategory_Product.Delete(_OProduct.ProductID);
                if (lstCatID != null)
                {
                    foreach (int IDCat in lstCatID)
                    {
                        _BCategory_Product.Add(IDCat, _OProduct.ProductID);
                    }
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// Lấy về ProductID của sản phẩm vừa thêm vào
        /// </summary>
        /// <returns></returns>
        public int GetIDLastRecord()
        {
            DataTable dtb = RunProcedureGet("sp_tblProduct_getidlastrecord");
            if (dtb.Rows.Count > 0)
            {
                return int.Parse(dtb.Rows[0]["ProductID"].ToString());
            }
            return 0;
        }
        /// <summary>
        /// Xóa sản phẩm
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public bool Delete(int ProductID)
        {
            _BCategory_Product.Delete(ProductID);
            SqlParameter[] sqlParam = { new SqlParameter("@ProductID", SqlDbType.Int) };
            sqlParam[0].Value = ProductID;
            return RunProcedure("sp_tblProduct_delete", sqlParam);
        }
    }
}
